﻿Public Class FrmGestionDispo
    Dim dispos As New List(Of DispoEmp)
    Dim noEmp As Integer
    Private Sub FrmGestionDispo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, BtnAnnuler.Click
        noEmp = FrmAcceuil.getEmploye.getEmploye
        LstDispo.Items.Clear()
        dispos.Clear()
        Dim dispo = From i In bd.Disponibilites Where i.Employe = noEmp Select i
        For Each dis In dispo
            Dim disspo As New DispoEmp
            disspo.HeureDebut = New Date(dis.HeureDebut.Value.Ticks)
            disspo.HeureFin = (New Date(dis.HeureDebut.Value.Ticks).Add(dis.Duree)).ToString
            disspo.Jour = dis.Jour
            dispos.Add(disspo)
            LstDispo.Items.Add(RSet(CboJour.Text, 8) & " de " & dispos(dispos.Count - 1).HeureDebut.ToShortTimeString & " à " & dispos(dispos.Count - 1).HeureFin.ToShortTimeString)
        Next
        BtnSauvegarder.Enabled = False
        BtnAnnuler.Enabled = False
        CboJour.SelectedIndex = 0
    End Sub

    Private Sub BtnAjouter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAjouter.Click
        If Not DtpHeureFin.Value > DtpHeureDebut.Value Then
            MsgBox("L'heure de fin doit être après l'heure de début")
            Exit Sub
        End If
        Dim disspo As New DispoEmp

        disspo.HeureDebut = DtpHeureDebut.Value.ToLongTimeString
        disspo.HeureFin = DtpHeureFin.Value.ToLongTimeString
        disspo.Jour = CboJour.SelectedIndex

        dispos.Add(disspo)

        LstDispo.Items.Add(RSet(CboJour.Text, 8) & " de " & dispos(dispos.Count - 1).HeureDebut.ToShortTimeString & " à " & dispos(dispos.Count - 1).HeureFin.ToShortTimeString)

        VerifierFusion()
        BtnSauvegarder.Enabled = True
        BtnAnnuler.Enabled = True
    End Sub

    Private Sub BtnEnlever_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEnlever.Click
        dispos.RemoveAt(LstDispo.SelectedIndex)
            LstDispo.Items.RemoveAt(LstDispo.SelectedIndex)
            BtnSauvegarder.Enabled = True
            BtnAnnuler.Enabled = True

    End Sub

    Private Sub BtnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEdit.Click
        If Not DtpHeureFin.Value > DtpHeureDebut.Value Then
            MsgBox("L'heure de fin doit être après l'heure de début")
            Exit Sub
        End If
        dispos(LstDispo.SelectedIndex).HeureDebut = DtpHeureDebut.Value.ToLongTimeString
        dispos(LstDispo.SelectedIndex).HeureFin = DtpHeureFin.Value.ToShortTimeString
        dispos(LstDispo.SelectedIndex).Jour = CboJour.SelectedIndex

        LstDispo.Items(LstDispo.SelectedIndex) = (RSet(CboJour.Text, 8) & " de " & dispos(LstDispo.SelectedIndex).HeureDebut.ToShortTimeString & " à " & dispos(LstDispo.SelectedIndex).HeureFin.ToShortTimeString)
        VerifierFusion()
        BtnSauvegarder.Enabled = True
        BtnAnnuler.Enabled = True
    End Sub

    Private Sub LstDispo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstDispo.SelectedIndexChanged
        If LstDispo.SelectedIndex = -1 Then
            BtnEnlever.Enabled = False
            BtnEdit.Enabled = False

        Else
            BtnEnlever.Enabled = True
            BtnEdit.Enabled = True
            DtpHeureDebut.Value = (Now - Now.TimeOfDay).Add(New TimeSpan(dispos(LstDispo.SelectedIndex).HeureDebut.Ticks))
            DtpHeureFin.Value = (Now - Now.TimeOfDay).Add(New TimeSpan(dispos(LstDispo.SelectedIndex).HeureFin.Ticks))
            CboJour.SelectedIndex = dispos(LstDispo.SelectedIndex).Jour
        End If

    End Sub

    Private Sub BtnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOk.Click
        Me.Close()
    End Sub

    Private Sub BtnSauvegarder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSauvegarder.Click
            Dim emp = (From x In bd.Employes Where x.ID = noEmp Select x).First
            bd.Disponibilites.DeleteAllOnSubmit(From x In bd.Disponibilites Where x.Employe = noEmp Select x)

            bd.SubmitChanges()
        For Each aDispo As DispoEmp In dispos
            Dim dispoAjoute As New DAL.Disponibilite
            dispoAjoute.Employe = noEmp
            dispoAjoute.HeureDebut = New TimeSpan(aDispo.HeureDebut.TimeOfDay.Ticks)
            dispoAjoute.Duree = New TimeSpan(aDispo.HeureFin.TimeOfDay.Ticks - aDispo.HeureDebut.TimeOfDay.Ticks)
            dispoAjoute.Jour = aDispo.Jour
            emp.Disponibilites.Add(dispoAjoute)
        Next

        bd.SubmitChanges()

        BtnSauvegarder.Enabled = False
        BtnAnnuler.Enabled = False

    End Sub

    Public Sub VerifierFusion()
        i = 0
        Do
            j = 0
            Do
                If Not i = j Then
                    'Prepend
                    If dispos(i).Jour = dispos(j).Jour AndAlso dispos(i).HeureDebut > dispos(j).HeureDebut AndAlso dispos(i).HeureDebut < dispos(j).HeureFin AndAlso dispos(i).HeureFin > dispos(j).HeureDebut Then
                        dispos(i).HeureDebut = dispos(j).HeureDebut
                        dispos.RemoveAt(j)
                        LstDispo.Items.RemoveAt(j)
                        i -= 1
                        Exit Do
                        ' Append
                    ElseIf dispos(i).Jour = dispos(j).Jour AndAlso dispos(i).HeureFin > dispos(j).HeureDebut AndAlso dispos(i).HeureFin > dispos(j).HeureFin AndAlso dispos(i).HeureDebut < dispos(j).HeureFin Then
                        dispos(i).HeureFin = dispos(j).HeureFin
                        dispos.RemoveAt(j)
                        LstDispo.Items.RemoveAt(j)
                        i -= 1
                        Exit Do
                    ElseIf dispos(i).Jour = dispos(j).Jour AndAlso dispos(i).HeureDebut = dispos(j).HeureFin Then
                        dispos(i).HeureDebut = dispos(j).HeureDebut
                        dispos.RemoveAt(j)
                        LstDispo.Items.RemoveAt(j)
                        i -= 1
                        Exit Do
                    ElseIf dispos(i).Jour = dispos(j).Jour AndAlso dispos(i).HeureFin = dispos(j).HeureDebut Then
                        dispos(i).HeureFin = dispos(j).HeureFin
                        dispos.RemoveAt(j)
                        LstDispo.Items.RemoveAt(j)
                        i -= 1
                        Exit Do
                    ElseIf dispos(i).Jour = dispos(j).Jour AndAlso dispos(i).HeureDebut = dispos(j).HeureDebut AndAlso dispos(i).HeureFin = dispos(j).HeureFin Then
                        dispos.RemoveAt(j)
                        LstDispo.Items.RemoveAt(j)
                        i -= 1
                    Else

                        j += 1
                    End If
                Else
                    j += 1
                End If
            Loop While j < dispos.Count
            i += 1
            j = 0
        Loop While i < dispos.Count
        actualiserListe()
    End Sub
    Sub actualiserListe()
        LstDispo.Items.Clear()
        For i = 0 To dispos.Count - 1
            CboJour.SelectedIndex = dispos(i).Jour
            LstDispo.Items.Add(RSet(CboJour.Text, 8) & " de " & dispos(i).HeureDebut.ToShortTimeString & " à " & dispos(i).HeureFin.ToShortTimeString)
        Next

    End Sub
End Class